/**
 * Created by Administrator on 2016/11/9 0009.
 */


var abandonUserList = new Object();
var timer = '';
var keepTime = 1000;
var tagEle = "querySelectorAll" in document ? document.querySelectorAll(".tag") : getClass("tag"),
    paper = "querySelectorAll" in document ? document.querySelector(".tagBall") : getClass("tagBall")[0];
var lottery = null;
RADIUS = 280,//调球大小
    fallLength = 500,
    tags = [],
    angleX = Math.PI / 250,
    angleY = Math.PI / 250,
    CX = paper.offsetWidth / 2,
    CY = paper.offsetHeight / 2,
    EX = paper.offsetLeft + document.body.scrollLeft + document.documentElement.scrollLeft,
    EY = paper.offsetTop + document.body.scrollTop + document.documentElement.scrollTop;
$(".start").css("left", CX - 50).css("top", CY - 50);

function getClass(className) {
    var ele = document.getElementsByTagName("*");
    var classEle = [];
    for (var i = 0; i < ele.length; i++) {
        var cn = ele[i].className;
        if (cn === className) {
            classEle.push(ele[i]);
        }
    }
    return classEle;
}

function innit() {
    for (var i = 0; i < tagEle.length; i++) {
        var a, b;
        var k = (2 * (i + 1) - 1) / tagEle.length - 1;
        var a = Math.acos(k);
        var b = a * Math.sqrt(tagEle.length * Math.PI);
        // var a = Math.random()*2*Math.PI;
        // var b = Math.random()*2*Math.PI;
        var x = RADIUS * Math.sin(a) * Math.cos(b);
        var y = RADIUS * Math.sin(a) * Math.sin(b);
        var z = RADIUS * Math.cos(a);
        var t = new tag(tagEle[i], x, y, z, tagEle[i].getAttribute("lotteryUniqid"));
        tagEle[i].style.color = "rgb(" + parseInt(Math.random() * 255) + "," + parseInt(Math.random() * 255) + "," + parseInt(Math.random() * 255) + ")";
        tags.push(t);
        t.initmove();
    }
}

Array.prototype.forEach = function (callback) {
    for (var i = 0; i < this.length; i++) {
        callback.call(this[i]);
    }
}

function animate() {

    timer = setInterval(function () {
        rotateX();
        rotateY();
        tags.forEach(function () {
            this.move();
        })
    }, 10)
}

if ("addEventListener" in window) {
//                paper.addEventListener("mousemove" , function(event){
//                    var x = event.clientX - EX - CX;
//                    var y = event.clientY - EY - CY;
//                    // angleY = -x* (Math.sqrt(Math.pow(x , 2) + Math.pow(y , 2)) > RADIUS/4 ? 0.0002 : 0.0001);
//                    // angleX = -y* (Math.sqrt(Math.pow(x , 2) + Math.pow(y , 2)) > RADIUS/4 ? 0.0002 : 0.0001);
//                    angleY = x*0.0001;
//                    angleX = y*0.0001;
//                });

    //paper.addEventListener("click",function(event){
    //    clearInterval(timer);
    //    tags.forEach(function(){
    //        this.end();
    //    });
    //});
}
else {
    paper.attachEvent("onmousemove", function (event) {
        var x = event.clientX - EX - CX;
        var y = event.clientY - EY - CY;
        angleY = x * 0.0001;
        angleX = y * 0.0001;
    });
}

function rotateX() {
    var cos = Math.cos(angleX);
    var sin = Math.sin(angleX);
    tags.forEach(function () {
        var y1 = this.y * cos - this.z * sin;
        var z1 = this.z * cos + this.y * sin;
        this.y = y1;
        this.z = z1;
    })

}

function rotateY() {
    var cos = Math.cos(angleY);
    var sin = Math.sin(angleY);
    tags.forEach(function () {
        var x1 = this.x * cos - this.z * sin;
        var z1 = this.z * cos + this.x * sin;
        this.x = x1;
        this.z = z1;
    })
}

var tag = function (ele, x, y, z, uniqid) {
    this.uniqid = uniqid;
    this.ele = ele;
    this.x = x;
    this.y = y;
    this.z = z;
}

tag.prototype = {
    initmove: function () {
        var scale = fallLength / (fallLength - this.z);
        var alpha = (this.z + RADIUS) / (2 * RADIUS);
        this.ele.style.fontSize = 15 * scale + "px";
        this.ele.style.opacity = alpha + 0.5;
        this.ele.style.filter = "alpha(opacity = " + (alpha + 0.5) * 100 + ")";
        this.ele.style.zIndex = parseInt(scale * 100);
        //this.ele.style.left = this.x + CX - this.ele.offsetWidth/2 +"px";
        //this.ele.style.top = this.y + CY - this.ele.offsetHeight/2 +"px";
        $(this.ele).animate({
            top: this.y + CY - this.ele.offsetHeight / 2 + "px",
            left: this.x + CX - this.ele.offsetWidth / 2 + "px"
        }, 1000);
    },
    move: function () {
        var scale = fallLength / (fallLength - this.z);

        var alpha = (this.z + RADIUS) / (3 * RADIUS);
        this.ele.style.width = 30 * scale + "px";
        this.ele.style.height = 30 * scale + "px";
        this.ele.style.opacity = alpha + 0.5;
        this.ele.style.filter = "alpha(opacity = " + (alpha + 0.5) * 100 + ")";
        this.ele.style.zIndex = parseInt(scale * 100);
        this.ele.style.left = this.x + CX - this.ele.offsetWidth / 2 + "px";
        this.ele.style.top = this.y + CY - this.ele.offsetHeight / 2 + "px";


        //$(this.ele).animate({top:this.y + CY - this.ele.offsetHeight/2 +"px",left:this.x + CX - this.ele.offsetWidth/2 +"px"},'slow');
    },
    end: function (lotteryUniqid,lotteryName) {
        var me = this;

        $(this.ele).animate({
            top: CY - 50 + 'px',
            left: CY - 50 + 'px',
            width: '100px',
            height: "100px"
        }, 1000, function (event) {

            //$(me.ele).css("box-shadow","0px 0px 0px #fff");


            if (me.uniqid == lotteryUniqid) {
               // $("#lotteryResult").append("<div class='lotteryUserBox'><img  uniqid='" + me.uniqid + "' src='" + $(me.ele).attr("src") + "' class='lotteryUser'/><div class='selectUser'><a class='abandence'>弃抽</a><a class='next'>下一个</a></div></div>");
                $("#lotteryResult").append('<div class="lotteryImg" ><img  uniqid= "'+ me.uniqid +'"src="'+ $(me.ele).attr("src") +'"></div>'+'<div class="lo-username"><span>'+lotteryName+'</span></div>'+'<div class="lotteryControl"><a class="abandence" >弃抽</a><a class="next" >下一个</a></div>');
                $(".lotteryControl").hide();
            }
            $(me.ele).hide();


        });
    }
}


function lotteryList() {
    $(".lotteryList").show();
    var ultop = $('.lotteryList ul').offset().top;
    var ulHeight = $('.lotteryList ul').outerHeight(true);
    $('.lotteryList ul').animate({top: "-=" + ulHeight / 2 + "px"}, 10000, "linear");
    setInterval(function () {
        if (parseInt($('.lotteryList ul').offset().top) == parseInt(ultop - ulHeight / 2)) {
            $('.lotteryList ul').offset({top: ultop});
            $('.lotteryList ul').animate({top: "-=" + ulHeight / 2 + "px"}, 10000, "linear");
        }
    }, 10);
}

function startLottery(postUrl) {


    tags.splice(0, tags.length);
    $(".tag").show();
    innit();

    setTimeout(function () {
        animate();
    }, 1000);


}


function reStartLottery(postUrl) {

    if (abandonUserList != {}) {

        $.post(postUrl, abandonUserList, function () {
            tags.splice(0, tags.length);
            $(".tag").show();
            innit();

            setTimeout(function () {
                animate();
            }, 1000);

        });
    }

}





